package com.d2rv.guide;

import com.d2rv.model.InstructionModelManager;
import com.d2rv.offscreen.OffScreen3D;
import com.d2rv.settings.Settings;
import processing.core.PApplet;

/**
 * This class is the manager of the class Instruction used to define and control
 * the current instruction.
 *
 * @author Darrieutort A., Dupouy A., Rossignol L., Van Poucke A.
 */
public class InstructionManager {

    private InstructionModelManager instructionmodelmanager;
    private InstructionArrow instructionarrow;
    private InstructionArrowTurnBack instructionarrowturnback;
    private int instruction;
    private int delay;
    private int previoustime;
    private PApplet parent;
    private boolean chronostarted;

    /**
     * Default constructor.
     *
     * @param parent the current application
     */
    public InstructionManager(PApplet parent) {
        this.instructionmodelmanager = new InstructionModelManager(parent);
        this.instructionarrow = new InstructionArrow(this.instructionmodelmanager.instructionarrowmodel());
        this.instructionarrowturnback = new InstructionArrowTurnBack(this.instructionmodelmanager.instructionturnbackmodel());
        this.instruction = -1;
        this.delay = Settings.getInstance().delayinstruction();
        this.previoustime = 0;
        this.parent = parent;
    }

    /**
     * Returns the current instruction to consider.
     *
     * @return the current instruction
     */
    public Instruction currentInstrution() {
        if (this.chronostarted) {
            if ((this.parent.millis() - this.previoustime) >= this.delay) {
                this.instruction = -1;
            }
        }
        if (this.instruction == 0) {
            this.instructionarrow.instructionToDraw(0);
            return this.instructionarrow;
        }
        if (this.instruction == 1) {
            this.instructionarrow.instructionToDraw(1);
            return this.instructionarrow;
        }
        if (this.instruction == 2) {
            this.instructionarrow.instructionToDraw(2);
            return this.instructionarrow;
        }
        if (this.instruction == 3) {
            return this.instructionarrowturnback;
        }
        return null;
    }

    /**
     * Draws the current instruction.
     *
     * @param offscreen the current offscreen
     */
    public void drawInstruction(OffScreen3D offscreen) {
        Instruction ins = currentInstrution();
        ins.draw(offscreen);
    }

    /**
     * Defines the insctruction to consider.
     *
     * @param dir instruction number
     */
    public void instructionInit(int dir) {
        startChrono();
        this.instruction = dir;
    }

    /**
     * Starts the chrono to display only a few time the instruction.
     */
    public void startChrono() {
        this.chronostarted = true;
        this.previoustime = this.parent.millis();
    }
}